﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Bamboo.Utils;

namespace Bamboo.ExeShell
{
    /// <summary>
    /// 7z
    /// </summary>
    public class SevenZ
    {
        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="zipfile"></param>
        /// <param name="outputPath"></param>
        /// <returns></returns>
        public static string Extra(string zipfile, string outputPath)
        {
            var srcFile = new FileInfo(zipfile);
            if (!srcFile.Exists) throw new Exception("待解压的文件不存在: " + srcFile.FullName);
            var destDir = new DirectoryInfo(outputPath);
            if (!destDir.Exists) destDir.Create();

            var c = new ProcessExecute();
            c.IgnoreError = true;
            c.FileName = "7za.exe";
            c.Arguments = $"x \"{zipfile}\" -o\"{outputPath}\" -bd";
            int exitCode = c.Run();
            if (exitCode > 0) throw new Exception($"解压出错: 压缩包={zipfile}, 输出目录={outputPath}\r\n{c.OutputText.ToString()}");
            return c.OutputText.ToString();
        }
    }
}
